<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>用户管理</title>
    <link rel="stylesheet" href="/static/frame/layui/css/layui.css">
    <!--<link rel="stylesheet" href="http://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css">-->
    <link rel="stylesheet" href="/static/frame/static/css/style.css">
    <link rel="icon" href="/static/frame/static/image/code.png">
</head>
<body class="body">

<!-- 工具集 -->
<div class="my-btn-box">
    <span class="fl">
        <!--<a class="layui-btn layui-btn-danger radius btn-delect" id="btn-delete-all">批量删除</a>-->
        <a class="layui-btn btn-add btn-default" id="btn-add">添加用户</a>
        <a class="layui-btn btn-add btn-default" id="btn-refresh"><i class="layui-icon">&#x1002;</i></a>
    </span>
    <span class="fr">
        <span class="layui-form-label">搜索条件：</span>
        <div class="layui-input-inline">
            <input type="text" id="username" autocomplete="off" placeholder="请输入搜索条件" class="layui-input" >
        </div>
        <button class="layui-btn mgl-20" id="btn-search">查询</button>
    </span>
</div>

<!-- 表格 -->
<div id="dateTable" lay-filter="userList"></div>

<script type="text/javascript" src="/static/frame/layui/layui.js"></script>
<script type="text/javascript" src="/static/js/index.js"></script>
<script type="text/javascript">

    // layui方法
    layui.use(['table', 'form', 'layer', 'vip_table'], function () {

        // 操作对象
        var form = layui.form
            , table = layui.table
            , layer = layui.layer
            , vipTable = layui.vip_table
            , $ = layui.jquery

        // 表格渲染
        var tableIns = table.render({
            elem: '#dateTable'                  //指定原始表格元素选择器（推荐id选择器）
            , height: vipTable.getFullHeight()    //容器高度
            , cols: [
                [                  //标题栏
                    {checkbox: true, sort: true, fixed: true, space: true}
                    , {field: 'id', title: 'ID', width: 80}
                    , {field: 'username', title: '用户名', width: 200}
                    , {field: 'productName', title: '推广产品', width: 120}
                    , {field: 'productCount', title: '总量', width: 80}
                    , {field: 'accountAmountCount', title: '总金额', width: 80}
                    , {field: 'settleAmountPushTime', title: '结算推送时间', width: 180}
                    , {field: 'lastSettleAmountTime', title: '最后结算时间', width: 180}
                    , {field: 'currentAmount', title: '当前余额', width: 90}
                    , {field: 'status', title: '状态', width: 120, templet: '#status-templet'}
                    , {field: 'yesterdayCount', title: '昨日量', width: 80}
                    , {title: '操作', width: 300, toolbar: '#barOption'} //这里的toolbar值是模板元素的选择器
                ]
            ]
            , id: 'dataCheck'
            , url: '/user/data'
            , method: 'get'
            , page: true
            , limits: [15, 30, 60, 90, 150, 300]
            , limit: 15 //默认采用30
            , loading: false
            , done: function (res, curr, count) {
                $("[data-field='id']").css('display', 'none');
            }
        });

        // 获取选中行
        table.on('checkbox(userList)', function (obj) {

        });

        //监听工具条
        table.on('tool(userList)', function (obj) {
            var data = obj.data;
            if (obj.event === 'clearAmount') {
                // 清空结算


                layer.confirm('确定要清空余额么?', function (index) {
                    layer.close(index);

                    $.ajax({
                        url: '/user/'+data.id,
                        type: 'put',
                        success:function(result){
                            setTimeout(tableIns.reload(),1000)
                        }
                    })


                })


            } else if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    layer.close(index);

                    $.ajax({
                        url: '/user/'+data.id,
                        type: 'delete',
                        success:function(result){

                        }
                    })

                    tableIns.reload();
                });
            } else if (obj.event === 'edit') {
                $.get('/user/edit/' + data.id,{} ,function(data){
                    layer.open({
                        type: 1
                        , title: '编辑用户'
                        , closeBtn: 1
                        , area: '400px;'
                        , shade: 0
                        , id: 'LAY_user_edit' //设定一个id，防止重复弹出
                        // , btn: ['添加', '取消']
                        , btnAlign: 'c'
                        , moveType: 1 //拖拽模式，0或者1
                        , content: data
                        // , skin: 'layui-layer-content'
                        // , yes: function (index, layero) {
                        //
                        // }
                    });
                })
            } else if (obj.event === 'settle') {
                $.get('/user/settle/' + data.id,{} ,function(data){
                    layer.open({
                        type: 1
                        , title: '设置结算'
                        , closeBtn: 1
                        , area: '400px;'
                        , shade: 0
                        , id: 'LAY_user_settl' //设定一个id，防止重复弹出
                        , btnAlign: 'c'
                        , moveType: 1 //拖拽模式，0或者1
                        , content: data
                    });
                })
            }
        });

        // 刷新
        $('#btn-refresh').on('click', function () {
            tableIns.reload({where:{}});
        });

        // 搜索
        $('#btn-search').on('click', function () {

            tableIns.reload({where:{
                    username:$("#username").val()
                }});
        });


        $('#btn-add').on('click', function () {

            $.get('/user/add',{} ,function(data){
                layer.open({
                    type: 1
                    , title: '添加用户' //不显示标题栏
                    , closeBtn: 1
                    , area: '450px;'
                    , shade: 0
                    , id: 'LAY_layuipro' //设定一个id，防止重复弹出
                    // , btn: ['添加', '取消']
                    , btnAlign: 'c'
                    , moveType: 1 //拖拽模式，0或者1
                    , content: data
                    // , skin: 'layui-layer-content'
                    // , yes: function (index, layero) {
                    //
                    // }
                });
            })


        });

    });
</script>
<!-- 表格操作按钮集 -->
<script type="text/html" id="barOption">
    <a class="layui-btn layui-btn-mini" lay-event="settle">设置结算</a>
    <a class="layui-btn layui-btn-mini layui-btn-normal" lay-event="clearAmount">清空余额</a>
    <a class="layui-btn layui-btn-mini layui-btn-normal" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-mini layui-btn-danger" lay-event="del">删除</a>
</script>


<!--状态 不同颜色-->
<script type="text/html" id="status-templet">
    {{#  if(d.status === '未结算') { }}
        <span style="color: red;">{{d.status}}</span>
    {{#  } else if(d.status === '已推送') { }}
        <span style="color: blue;">{{d.status}}</span>
    {{#  } else { }}
        <span style="color: purple;">{{d.status}}</span>
    {{#  } }}
</script>


<script type="text/css">
    .layui-layer-content {
        overflow: visible;
    }
</script>

</body>


</html>